﻿using UnityEngine;
using Network;
using XLua;
using Framework.AssetsManager;
using Framework;
using System.Collections;
using LitJson;

[XLua.CSharpCallLua]
public delegate double LuaMax(double a, double b);

public class HotfixCalc
{
    public int Add(int a, int b)
    {
        return a - b;
    }

    public Vector3 Add(Vector3 a, Vector3 b)
    {
        return a - b;
    }
}

public class ProcInit : Proc
{
    public ProcInit()
        : base(ProcType.PROC_INIT)
    {
        
    }

    public override void process()
    {
        NetManager.Instance.Tick(Time.deltaTime);
    }

    public override void onBegin(object[] param)
    {
        IsInit = true;
        Mainloop.Instance.luaenv = new LuaEnv();
        Mainloop.Instance.luaenv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
        Mainloop.Instance.luaenv.AddLoader((ref string filename) =>
        {
            filename = filename.Replace(".", "/");
            filename = "Lua/" + filename;
            TextAsset ta = (TextAsset)RM.Instance.GetResource(filename);
            return ta.bytes;
        });
#if HOTFIX_ENABLE
        //hotfix
        TextAsset luaScript = RM.Instance.GetResource("Lua/Hotfix") as TextAsset;
        Mainloop.Instance.luaenv.DoString(luaScript.text);

        HotfixCalc fixedClass = new HotfixCalc();
        if(fixedClass.Add(10, 20) != 30)
            Debug.LogError("Hotfix failed!!");
#endif

        TableManager.GetInstance().Init();

        RegistNetworkEvent();
        Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, "0.0.0.0", (ushort)0, (uint)0);
    }

    public override void onEnd()
    {
        UnregistNetworkEvent();
    }

    void RegistNetworkEvent()
    {
        
    }

    void UnregistNetworkEvent()
    {
        
    }

    public override void onTcpDisconnected()
    {
    }
}
